Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

260
Views
returning [object HTMLDivElement] when using document.getElementById

I am inserting this into my html div:

<script>
    document.getElementById("mArray").appendChild = '<%= mArray %>'
    console.log('<%= mArray %>')

  </script>
  <div id="mArray"></div>

then, inside my js file, I have this:

var mArray = document.getElementById("mArray")
console.log("33: " + mArray)

but when I log it, I get this:

33: [object HTMLDivElement]

i want it to return the array data, which should look like [1,3,5,7,9]

what am i doing wrong?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

When reading, use

var mArray = document.getElementById("mArray").textContent

and if you want to use it as a JS array (assuming it is a valid js array format), you could do

var mArrayContents = document.getElementById("mArray").textContent;
var myArray = JSON.parse( mArrayContents );

Additionally, when setting the value you do not have to use JS.
You can do it directly

<div id="mArray"><%= mArray %></div>
about 4 years ago · Juan Pablo Isaza Report

0

Right now you are logging the element itself, you need to check for the data inside. Depending on how the information is stored you could possibly get the information with something such as

document.getElementById("mArray").innerHTML;

or

document.getElementById("mArray").value;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!